package com.aba.cms.teacher;


import java.util.List;
import java.util.Scanner;

public class TeacherController {
    private TeacherService teacherService = new TeacherService();
    private TeacherView teacherView = new TeacherView();

    // 处理教师管理
    public void manageTeachers() {
        int choice;
        
        do {
            choice = teacherView.showMenu();
            
            switch (choice) {
                case 1:
                    addTeacher();
                    break;
                case 2:
                    editTeacher();
                    break;
                case 3:
                    searchTeachers();
                    break;
                case 4:
                    listAllTeachers();
                    break;
                case 5:
                    teacherView.showMessage("退出系统，再见！");
                    break;
                default:
                    teacherView.showMessage("无效选择，请重新输入！");
            }
        } while (choice != 5);
    }

    // 添加教师
    private void addTeacher() {
        Teacher teacher = teacherView.getTeacherInput();
        teacherService.addTeacher(teacher);
        teacherView.showMessage("教师添加成功！");
    }

    // 编辑教师
    private void editTeacher() {
        int teacherId = teacherView.getTeacherId();
        Teacher teacher = teacherService.getTeacherById(teacherId);
        
        if (teacher == null) {
            teacherView.showMessage("未找到该教师！");
            return;
        }
        
        teacherView.showMessage("当前教师信息:");
        teacherView.displayTeacher(teacher);
        
        teacherView.showMessage("请输入新的教师信息，不修改的字段直接回车跳过:");
        
        Teacher newTeacherInfo = teacherView.getTeacherInput();
        
        // 更新教师信息
        if (!newTeacherInfo.getName().isEmpty()) {
            teacher.setName(newTeacherInfo.getName());
        }
        if (!newTeacherInfo.getDepartment().isEmpty()) {
            teacher.setDepartment(newTeacherInfo.getDepartment());
        }
        if (!newTeacherInfo.getTitle().isEmpty()) {
            teacher.setTitle(newTeacherInfo.getTitle());
        }
        if (!newTeacherInfo.getPhone().isEmpty()) {
            teacher.setPhone(newTeacherInfo.getPhone());
        }
        if (!newTeacherInfo.getEmail().isEmpty()) {
            teacher.setEmail(newTeacherInfo.getEmail());
        }
        
        teacherService.updateTeacher(teacher);
        teacherView.showMessage("教师信息更新成功！");
    }

    // 搜索教师
    private void searchTeachers() {
        String keyword = teacherView.getSearchKeyword();
        List<Teacher> teachers = teacherService.searchTeachersByName(keyword);
        
        if (teachers.isEmpty()) {
            teacherView.showMessage("未找到相关教师！");
        } else {
            teacherView.displayTeachers(teachers);
            
            System.out.print("是否查看某位教师的详细信息？(y/n): ");
            String choice = new Scanner(System.in).nextLine();
            
            if (choice.equalsIgnoreCase("y")) {
                int teacherId = teacherView.getTeacherId();
                Teacher teacher = teacherService.getTeacherById(teacherId);
                
                if (teacher != null) {
                    teacherView.displayTeacher(teacher);
                } else {
                    teacherView.showMessage("未找到该教师！");
                }
            }
        }
    }

    // 列出所有教师
    private void listAllTeachers() {
        List<Teacher> teachers = teacherService.getAllTeachers();
        
        if (teachers.isEmpty()) {
            teacherView.showMessage("暂无教师信息！");
        } else {
            teacherView.displayTeachers(teachers);
            
            System.out.print("是否查看某位教师的详细信息？(y/n): ");
            String choice = new Scanner(System.in).nextLine();
            
            if (choice.equalsIgnoreCase("y")) {
                int teacherId = teacherView.getTeacherId();
                Teacher teacher = teacherService.getTeacherById(teacherId);
                
                if (teacher != null) {
                    teacherView.displayTeacher(teacher);
                } else {
                    teacherView.showMessage("未找到该教师！");
                }
            }
        }
    }
}    